//
//  DetailFirstViewController.m
//  B
//
//  Created by Sunpeng on 15/11/6.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "DetailFirstViewController.h"
#import "DetailFirstTableViewCell.h"
#import "DetailSecondTableViewCell.h"
#import "HTTPTool.h"
#import "UIImageView+WebCache.h"
#import "ImageViewController.h"
#import "BaseView.h"


@interface DetailFirstViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,retain)NSMutableArray *oneArray;
@property(nonatomic,retain)UIImageView  *imageViews;
@end

@implementation DetailFirstViewController
//-(void)viewWillAppear:(BOOL)animated {
//    
//    [super viewWillAppear:animated];
//    for (UIView *view in self.tabBarController.view.subviews) {
//        if ([view isKindOfClass:[BaseView class]]) {
//            view.hidden = YES;
//        }
//    }
//}
//
//- (void) viewWillDisappear:(BOOL)animated {
//    
//    [super viewWillDisappear:animated];
//    for (UIView *view in self.tabBarController.view.subviews) {
//        if ([view isKindOfClass:[BaseView class]]) {
//            view.hidden = NO;
//            
//        }
//    }
//    
//    
//}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    //数组初始化
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        
    self.hidesBottomBarWhenPushed = YES;
    
    }
    
    return self;
}

//- (void)viewDidAppear:(BOOL)animated {
//    [super viewDidAppear:animated];
//    [self.navigationController.navigationBar setTranslucent:YES];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    
   self.view.backgroundColor  =[UIColor colorWithRed:251/255.0 green:247 /255.0 blue:236/255.0 alpha:1];
    self.view.backgroundColor = [UIColor whiteColor];
    NSDictionary *dict=[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    
    //返回键
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    self.navigationController.navigationBar.titleTextAttributes=dict;
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT-64) style: UITableViewStylePlain];
    self.myTableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    
    [self.myTableView setShowsVerticalScrollIndicator:NO];
    
    self.myTableView.delegate =self;
    
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
   
   
    
   
    
    
    
     self.oneArray = [NSMutableArray array];
    
   
     NSLog(@"3333");
    
    [self getafnData];
    
    
    
    
}
-(void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:NO];
}
-(void)getafnData{
    NSLog(@"666666777%@%@",self.typeNUmber,self.idString);
    
    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",self.typeNUmber,self.idString] body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
       NSLog(@"----http://api.breadtrip.com/destination/place/%@/%@/",self.typeNUmber,self.idString);
//        NSLog(@"222%@",dic);
//       NSArray *arr=dic[@"hottest_places"];
       
        DetaiModel *detailM =[[DetaiModel alloc]init];
        
        [detailM setValuesForKeysWithDictionary:dic];
        detailM.message = [dic objectForKey:@"description"];
        [self.oneArray addObject:detailM];
//        NSLog(@"1111%@",self.oneArray);
        //设置tableview的contentView距离上边界200
        //相对于0点,已经向下偏移了-200
//        self.myTableView.contentInset = UIEdgeInsetsMake(300.0*XWIDTH, 0, 0, 0);
        
        //相对于0点,图片坐标应该是(0,-200)
        self.imageViews = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200.0*XHIGHT, 375*XWIDTH, 200.0*XHIGHT)];
        [self.imageViews sd_setImageWithURL:[NSURL URLWithString:self.url] placeholderImage:nil];
        //设置imageView高度改变时宽度也跟着改变
        self.imageViews.contentMode = UIViewContentModeScaleAspectFill;
        self.imageViews.layer.masksToBounds = YES;
        [self.myTableView insertSubview:self.imageViews atIndex:0];
        //imageView添加点击事件
        [self.imageViews setUserInteractionEnabled:YES];
        [self.imageViews addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCategory:)]];
        [self.myTableView reloadData];
      
        
    }
     
     
    
     
     
          failure:^(NSError *error) {
              //NSLog(@"%@",error);
          }];

    
    


}
-(void)clickCategory:(UITapGestureRecognizer *)gestureRecognizer
{
    NSLog(@"click");
    ImageViewController *detailVC = [[ImageViewController alloc]init];
    detailVC.idString = self.idString;
    detailVC.typeNUmber = self.typeNUmber;

    [self.navigationController pushViewController:detailVC animated:YES];
        
        
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //NSLog(@"8888%d",self.allArray.count);
    if (section == 0) {
        return self.oneArray.count;
    }else{
    
    return 6;
    
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 190*XHIGHT;
    }else{
        if (indexPath.row==0) {
            return 160*XHIGHT;
        }else{
        

    return 100*XHIGHT;
        }
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==0) {
        
    
    static NSString *cellIndentifier = @"cell1";
    DetailFirstTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell1 == nil)
    {
        cell1= [[DetailFirstTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell1.selectionStyle = UITableViewCellSelectionStyleNone;
         cell1.backgroundColor  =[UIColor whiteColor];
//    PriceModel *priceM = [self.allArray objectAtIndex:indexPath.row];
//    cell.priceM = priceM;
//        if (self.oneArray.count != 0) {
      DetaiModel *detailM = self.oneArray[indexPath.row];
      cell1.detailM = detailM;
//        }
//
        
        
    return cell1;
    }else{
        static NSString *cellIndentifier = @"cell2";
        DetailSecondTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if (cell2 == nil)
        {
            cell2= [[DetailSecondTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        }
        cell2.selectionStyle = UITableViewCellSelectionStyleNone;
         cell2.backgroundColor  =[UIColor whiteColor];
        
        
        if (self.oneArray.count != 0){
    DetaiModel *detailM = self.oneArray[0];
            
            
        if (indexPath.row == 0) {
            cell2.firstLabel.text =@"概况";
            cell2.thirdLabel.text = detailM.message;
        }if (indexPath.row == 1) {
            cell2.firstLabel.text = @"地址";
            cell2.secondlbel.text = detailM.address;
        }if (indexPath.row == 2) {
            cell2.firstLabel.text = @"到达方式";
            cell2.secondlbel.text = detailM.arrival_type;
        }if (indexPath.row == 3) {
            cell2.firstLabel.text = @"开放时间";
            if (detailM.opening_time.length==0) {
                cell2.textLabel.text=@"全天";
            }else{
            cell2.secondlbel.text = detailM.opening_time;
            }
        }if (indexPath.row == 4) {
            cell2.firstLabel.text = @"联系方式";
            if (detailM.tel.length==0) {
                cell2.secondlbel.text =@"+34-946836432";
            }else{
            cell2.secondlbel.text = detailM.tel;
            }
        }if (indexPath.row == 5) {
            cell2.firstLabel.text = @"官方网站";
            if (detailM.website.length == 0) {
                cell2.secondlbel.text = @"http://www.sagradafamilan.cat/";
            }else{
            cell2.secondlbel.text = detailM.website;
            }
        }
        
    }
        return cell2;
        
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    DetailViewController *detailVC = [[DetailViewController alloc]init];
//    PriceModel *priceM = [self.allArray objectAtIndex:indexPath.row];
//    
//    detailVC.id = priceM.id;
//    [self.navigationController pushViewController:detailVC animated:YES];
    
    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y ;//加上导航栏高度,第一次是-200
    NSLog(@"y2 === %f",y);
    
    if (y < -200*XHIGHT) {
        CGRect frame = self.imageViews.frame;
        frame.origin.y = y;//imageView的frame是不断往上偏移
        frame.size.height =  200*XHIGHT - y;//tablview向下偏移了多少,高度就增加多少
        self.imageViews.frame = frame;
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
